<%@ page language="java" import="java.util.*" import="java.sql.*" import="orm.*" pageEncoding="utf-8" errorPage="errorPage.jsp"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%@  page import ="org.apache.struts.util.LabelValueBean"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
   	if(session.getAttribute("Student") == null)
       	response.sendRedirect(path + "/index.jsp");
   	Vector v = new Vector();
   	Vector v2 = new Vector();
   	Vector v3 = new Vector();
   	Set courseTest = new HashSet();
   	Set testTemplate = new HashSet();
   	if(request.getAttribute("courseList") == null){
   		v.add(new LabelValueBean("对不起，你没有课程！",""));
   	}
  	else{
   		List courseList = (List)request.getAttribute("courseList");
   		for(int i = 0; courseList != null && i < courseList.size();i++){
       		Course c = (Course)courseList.get(i);
       		v.add(new LabelValueBean(c.getCoursename(),String.valueOf(c.getCourseid())));
       	}

   		if(v != null && v.size() > 0){
       		Map testMap = (Map)request.getAttribute("testMap");
       		Course cs=(Course)courseList.get(0);
       		courseTest =(Set)(testMap.get(cs.getCoursename()));
       		if (courseTest != null && courseTest.size() > 0){
       			for (Iterator it = courseTest.iterator(); it.hasNext();){
       				Test t = (Test)it.next();
       				v2.add(new LabelValueBean(t.getTestname(), String.valueOf(t.getTestid())));
       			}
       			Map templateMap = (Map)request.getAttribute("templateMap");
       			Test t = (Test)courseTest.iterator().next();
       			testTemplate = (Set)templateMap.get(new Long(t.getTestid()));
       			if (testTemplate != null && testTemplate.size() > 0){
       				for (Iterator it = testTemplate.iterator(); it.hasNext(); ){
       					Testtemplate temp = (Testtemplate)it.next();
       					v3.add(new LabelValueBean(temp.getTempName(), String.valueOf(temp.getTempid())));
       				}
       			}
       			else{
       				request.removeAttribute("templateMap");
       				v3.add(new LabelValueBean("对不起，当前考试没有模板！", ""));
       			}
       		}
       		else {
				v2.add(new LabelValueBean("对不起，当前课程当前时间没有考试！", ""));
       		}
   		}
   		
   }
   pageContext.setAttribute("v",v); 
   pageContext.setAttribute("v2",v2);   
   pageContext.setAttribute("v3",v3);   
 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href = <%=basePath%> />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>考试中心</title>
<link href="css/sStyles.css" rel="stylesheet" type="text/css" />
<% User stu  =(User) session.getAttribute("Student");%>
<script type="text/javascript">
	function startTest(){
		var flag = confirm('注意：进入考试后将不能退出重考，确定开始考试么？'); 
		return flag;
	}
</script>
<script language="javascript" src="js/AjaxRequest.js">
</script>
<script type="text/javascript">
    function showTests(){
		var loader=new net.AjaxRequest("startexam.do?action=changeCourse&c=" + course.value,showTest,onerror,"GET");
	}
	function showTemplates(){
		var loader2=new net.AjaxRequest("startexam.do?action=changeTest&t=" + testS.value,showTemp,onerror,"GET");
	}
	function display(flag){
		var loader3=new net.AjaxRequest("startexam.do?action=display",displayButton,onerror,"GET");
	}
	function onerror(){
	}
	function showTest(){
		testS.innerHTML=this.req.responseText;
		showTemplates();
	}
	function showTemp(){
		template.innerHTML=this.req.responseText;
		display();
	}
	function displayButton(){
		var str = this.req.responseText;
		var t = document.getElementById("startBtn");
		if (str.indexOf("display")!= -1){
			if (t.rows.length != 0){
				t.deleteRow(0);
			}
			var r = t.insertRow(0);
			r.insertCell(0).innerHTML = "请选择试卷模板：";
			r.insertCell(1).innerHTML = "<input type='submit' name='start' style='height:30px' value='确定' />";
		}
		else{
			if (t.length != 0){
				t.deleteRow(0);
			}
		}
	}
</script>
</head>

<body>
	<!-- CONTAINER START -->
<div id = "container">
<!-- HEADER START -->
        <div id = "top">
        	<div id = "navigation">
        	    <html:link forward = "go_to_student" styleClass="link">&nbsp;&nbsp;&nbsp;&nbsp;首页</html:link>
        	    <html:link forward = "go_to_stu_notice" styleClass="link">&nbsp;&nbsp;&nbsp;&nbsp;通知</html:link>
        	    <html:link href="startexam.do?action=selectcourse" styleClass="link">&nbsp;&nbsp;&nbsp;&nbsp;考试中心 </html:link>
                <html:link page="/index.jsp" styleClass="link">&nbsp;&nbsp;&nbsp;&nbsp;成绩查询</html:link>
                <html:link page="/message.jsp" styleClass="link">&nbsp;&nbsp;&nbsp;&nbsp;留言板</html:link>
            </div>
        </div>
        <!-- HEADER END -->
        
    <!-- LOGIN START -->
        <div id = "login">
        	<div id = "name">
        	<%=stu.getRealname() %>
            </div>
            <html:link page="/student/studentInfo.jsp" styleClass="viewInfo">·查询个人信息</html:link>
        	<p><html:link page="/student/stuModifyPwd.jsp" styleClass="modifyPwd">·修改密码</html:link></p>
        	<p><html:link page="/student/letter.jsp" styleClass="letter">·站内信</html:link></p>
            <p><html:link forward="logout" styleClass="logout">·注销登陆</html:link></p>

  </div>
        <!-- LOGIN END -->
        
        <!-- CLOCK START -->
  <div id = "clock">
        	<iframe src="index/clock.html" height="407" width="296" frameborder="0" scrolling="no"
            	marginheight="0" marginwidth="0" id="clockFrame" name="clockFrame"></iframe>
</div>
        <!-- CLOCK END -->
        
        <!-- MAIN START -->
      	<div id = "main">
       	  <div id = "test">
   	      <html:form  method="post"  action="startexam.do?action=startexam" onsubmit="return startTest();">
            	<table width="450" border="1">
  					<tr >
    					 <td align="left" valign="top" >请选择科目：</td>
    					 <td>
    					   <html:select property="courseIndex" styleId="course" onchange="showTests();">
    					   <html:options collection="v" property="value" labelProperty="label" />
	                       </html:select></td>               
  					</tr>
  					<tr>
  						<td align="left" valign="top" >请选择考试：</td>
  						<td>
    					   <html:select property="testIndex" styleId="testS" onchange="showTemplates();">
    					   <html:options collection="v2" property="value" labelProperty="label" />
	                       </html:select></td>
  					</tr>
  					<tr>
    					 <td align="left" valign="top" >请选择试卷模板：</td>
    					 <td>
    					  	<html:select property="templateid" styleId="template"  >
	                    	<html:options collection="v3" property="value" labelProperty="label" />
	                       	</html:select></td>
  					</tr>
				</table>
                <table width="450" id="startBtn"> 
           	     <tr><%if (testTemplate != null && testTemplate.size() > 0){%>
               	      <td width="313" align="right">生成试卷开始考试？</td>
                      <td width="125"><input type="submit" name="start" 
                  						     style="height:30px" value="确定" /></td><%} %>
                  </tr>
               </table>
     </html:form>
          </div>
   	  </div>
   	<!-- MAIN END -->
        
   	<!-- FOOTER START -->
   	<div id = "footer">
  </div>
   	<!-- FOOTER END -->
        
  </div>
    <!-- CONTAINER END -->
    
</body>
</html>
